﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;


namespace 赛马
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Form.CheckForIllegalCrossThreadCalls = false;
        }
        public string name;
        //调用Person类里面的数据，定义甲乙丙对应的对象，包含名字跟筹码
        internal Person aPerson = new Person("甲", 100);
        internal Person bPerson = new Person("乙", 100);
        internal Person cPerson = new Person("丙", 100);
        //调用Horse类里面的数据，定义三匹马的马号
        internal Horse aHorse = new Horse(1);
        internal Horse bHorse = new Horse(2);
        internal Horse cHorse = new Horse(3);
        //定义三个结构体对象J,Y,B
        Bet.bundle J, Y, B;

        

        private void xiazhubutton_Click(object sender, EventArgs e)
        {
            if (name == aPerson.name && numericUpDown1.Value >= 5)
            {
                if (numericUpDown1.Value <= aPerson.chouma)
                {
                    J.person = aPerson;
                    //判断甲所选马的马号
                    if (numericUpDown2.Value == 1)
                    {
                        J.horse = aHorse;
                    }
                    else if (numericUpDown2.Value == 2)
                    {
                        J.horse = bHorse;
                    }
                    else if (numericUpDown2.Value == 3)
                    {
                        J.horse = cHorse;
                    }
                    Bet.chouma1 = (int)numericUpDown1.Value;
                    jia.Text = "已投" + J.horse.mahao.ToString() + "号";
                    aPerson.chouma = aPerson.chouma - Bet.chouma1;
                    jiachouma.Text = Convert.ToString(aPerson.chouma);
                    start.Enabled = true;
                    xiazhubutton.Enabled = false;
                }
                else
                {
                    MessageBox.Show("筹码余额不足！");
                }
            }
            if (name == bPerson.name && numericUpDown1.Value >= 5)
                {
                    if (numericUpDown1.Value <= bPerson.chouma)
                    {
                        Y.person = bPerson;
                        //判断乙所选马的马号
                        if (numericUpDown2.Value == 1)
                        {
                            Y.horse = aHorse;
                        }
                        else if (numericUpDown2.Value == 2)
                        {
                            Y.horse = bHorse;
                        }
                        else if (numericUpDown2.Value == 3)
                        {
                            Y.horse = cHorse;
                        }
                        Bet.chouma2 = (int)numericUpDown1.Value;
                        bPerson.chouma = bPerson.chouma - Bet.chouma2;
                        yichouma.Text = Convert.ToString(bPerson.chouma);
                        yi.Text = "已投" + Y.horse.mahao.ToString() + "号";
                        start.Enabled = true;
                        xiazhubutton.Enabled = false;
                    }
                    else
                    {
                        MessageBox.Show("筹码余额不足！");
                    }
                }
            if (name == cPerson.name && numericUpDown1.Value >= 5)
                {
                    if (numericUpDown1.Value <= cPerson.chouma)
                    {
                        B.person = cPerson;
                        //判断丙所选马的马号
                        if (numericUpDown2.Value == 1)
                        {
                            B.horse = aHorse;
                        }
                        else if (numericUpDown2.Value == 2)
                        {
                            B.horse = bHorse;
                        }
                        else if (numericUpDown2.Value == 3)
                        {
                            B.horse = cHorse;
                        }
                        Bet.chouma3 = (int)numericUpDown1.Value;
                        cPerson.chouma = cPerson.chouma - Bet.chouma3;
                        bingchouma.Text = Convert.ToString(cPerson.chouma);
                        bing.Text = "已投" + B.horse.mahao.ToString() + "号";
                        start.Enabled = true;
                        xiazhubutton.Enabled = false;
                    }
                    else
                    {
                        MessageBox.Show("筹码余额不足！");
                    }
                }
             if (numericUpDown1.Value < 5)
                { 
                    MessageBox.Show("最少下注5个筹码！");
                }
                
        }

        private void jiaradioButton_CheckedChanged(object sender, EventArgs e)
        {
            //判断甲是否已投注
            if (Bet.chouma1 > 0)
            {
                xiazhubutton.Enabled = false;
            }
            else
            {
                xiazhubutton.Enabled = true;
            }
            name = aPerson.name;
            xianshi.Text = aPerson.name;
            
        }

        private void yiradioButton_CheckedChanged(object sender, EventArgs e)
        {
            //判断乙是否已投注
            if (Bet.chouma2 > 0)
            {
                xiazhubutton.Enabled = false;
            }
            else
            {
                xiazhubutton.Enabled = true;
            }
            name = bPerson.name;
            xianshi.Text = bPerson.name;
            
        }

        private void bingradioButton_CheckedChanged(object sender, EventArgs e)
        {
            //判断丙是否已投注
            if (Bet.chouma3 > 0)
            {
                xiazhubutton.Enabled = false;
            }
            else
            {
                xiazhubutton.Enabled = true;
            }
            name = cPerson.name;
            xianshi.Text = cPerson.name;
            
        }
        public void start_Click(object sender, EventArgs e)
        {
            //设定三匹马起跑点的位置
            aHorse.jvli = 88;
            bHorse.jvli = 88;
            cHorse.jvli = 88;
            new System.Threading.Thread(Test).Start();//比赛开始  
        }
        public void Test()
        {
            this.WindowsMediaPlayer.URL = "1763.wav";//播放bgm
            Random radom = new Random();//生成一个随机数
            start.Enabled = false;
            xiazhubutton.Enabled = false;
            while ((pictureBox4.Left < 686) && (pictureBox5.Left < 686) && (pictureBox6.Left < 686))//当其中一匹马到达终点比赛结束
            {
                //调用Horse类中的Run方法，
                aHorse.Run(radom.Next(1, 15));
                bHorse.Run(radom.Next(1, 15));
                cHorse.Run(radom.Next(1, 15));
                pictureBox4.Left = aHorse.jvli;
                pictureBox5.Left = bHorse.jvli;
                pictureBox6.Left = cHorse.jvli;
                System.Threading.Thread.Sleep(80);//比赛结束
            }
            WindowsMediaPlayer.Ctlcontrols.stop();//bgm停止播放
            Bet.jisuan(this,J,Y,B);//执行Bet类中的jisuan方法
            jiachouma.Text = Convert.ToString(aPerson.chouma);
            yichouma.Text = Convert.ToString(bPerson.chouma);
            bingchouma.Text = Convert.ToString(cPerson.chouma);
            jia.Text = "未投注";
            yi.Text = "未投注";
            bing.Text = "未投注";
            //让三匹马回归起点
            System.Threading.Thread.Sleep(1000);
            pictureBox4.SetBounds(88, 56, 118, 55);
            pictureBox5.SetBounds(88, 138, 118, 55);
            pictureBox6.SetBounds(88, 220, 118, 55);
            //把投注的筹码归零
            Bet.chouma1 = 0;
            Bet.chouma2 = 0;
            Bet.chouma3 = 0;
            start.Enabled = true;
            xiazhubutton.Enabled = true;
           
        }
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //甲乙丙所选马号设置一个初始值
            J.horse = new Horse(0);
            Y.horse = new Horse(0);
            B.horse = new Horse(0);
        }

        private void jiachouma_Click(object sender, EventArgs e)
        {

        }

        private void yichouma_Click(object sender, EventArgs e)
        {

        }

        private void bingchouma_Click(object sender, EventArgs e)
        {

        }

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {
             
        }

        private void exitbutton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
